﻿#include "ManectricBlest.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"
#include "../../MirEnvir/Envir.h"
#include "../../MirEnvir/Map.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    ManectricBlest::ManectricBlest(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void ManectricBlest::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (_attackCount >= 5)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 2;
            Broadcast(tempVar);

            int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);

            std::vector<MapObject*> targets = FindAllTargets(3, getCurrentLocation());

            for (int i = 0; i < targets.size(); i++)
            {
                targets[i]->Attacked(this, damage, DefenceType::MAC);

                if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= targets[i]->Stats[Stat::PoisonResist])
                {
                    if (getEnvir()->Random->Next(5) == 0)
                    {
                        Poison *tempVar2 = new Poison(), *this;
                        tempVar2->PType = PoisonType::Frozen;
                        tempVar2->Duration = 5;
                        tempVar2->TickSpeed = 1000;
                        targets[i]->ApplyPoison(tempVar2, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                    }
                }
            }

            _attackCount = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        switch (getEnvir()->Random->Next(3))
        {
            case 0:
            case 1:
                _attackCount++;
                MonsterObject::Attack();
                break;
            case 2:
            {
                    int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                    S::ObjectAttack *tempVar3 = new S::ObjectAttack();
                    tempVar3->ObjectID = ObjectID;
                    tempVar3->Direction = getDirection();
                    tempVar3->Location = getCurrentLocation();
                    tempVar3->Type = 1;
                    Broadcast(tempVar3);

                    MirDirection dir = Functions::PreviousDir(getDirection());
                    Point *tar;
                    Cell *cell;

                    for (int i = 0; i < 8; i++)
                    {
                        tar = Functions::PointMove(getCurrentLocation(), dir, 1);
                        dir = Functions::NextDir(dir);
                        if (tar == getFront())
                        {
                            continue;
                        }

                        if (!getCurrentMap()->ValidPoint(tar))
                        {
                            continue;
                        }

                        cell = getCurrentMap()->GetCell(tar);

                        if (cell->Objects.empty())
                        {
                            continue;
                        }

                        for (int o = 0; o < cell->Objects.size(); o++)
                        {
                            MapObject *ob = cell->Objects[o];
                            if (ob->getRace() != ObjectType::Player && ob->getRace() != ObjectType::Monster)
                            {
                                continue;
                            }
                            if (!ob->IsAttackTarget(this))
                            {
                                continue;
                            }

                            ob->Attacked(this, damage, DefenceType::Agility);
                            break;
                        }
                    }
                    break;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;
    }
}
